﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MINERP.MODEL.EM;
using MINERP.BLL.EM;
using MINERP.COM;

namespace MINERP.ComControls
{
    public partial class frmChat : Form
    {
        private string _ChatSendStyle = "我对[{0}]说 [发送时间:{1}] \r\n";
        private string _ChatReceiveStyle = "[{0}]对您说 (发送时间：{1} \r\n";
        public event EventHandler SetTreeUser;
        public event GetSendListHandler getSendList;
        private bool showRecords = true;
        public frmChat()
        {
            InitializeComponent();
        }

        private UsersInfo _CurrentFriend;
        /// <summary>
        /// 当前好友信息
        /// </summary>
        public UsersInfo CurrentFriend
        {
            get { return _CurrentFriend; }
            set
            { 
                _CurrentFriend = value;
                DisplayUser();
            }
        }

        private List<UsersInfo> _FriendList;
        /// <summary>
        /// 所有好友
        /// </summary>
        public List<UsersInfo> FriendList
        {
            get { return _FriendList; }
            set 
            {
                _FriendList = value;
            }
 
        }
        private MessagesInfo _ReceiveMessage;
        /// <summary>
        /// 接收到的消息
        /// </summary>
        public MessagesInfo ReceiveMessage
        {
            get { return _ReceiveMessage; }
            set 
            { 
                _ReceiveMessage = value;
                _CurrentFriend = _ReceiveMessage.FromUser;
                DisplayUser();
                ShowMessage();
            }
        }

        private void ShowMessage()
        {
            Messages.SetMessageState(_ReceiveMessage.Id, EnumMessageState.Readed);
            this.txtMsgHistory.AppendText(string.Format(_ChatReceiveStyle,_CurrentFriend.NickName, _ReceiveMessage.MessageTime.ToString()));
            this.txtMsgHistory.AppendText(_ReceiveMessage.Message + "\r\n");
        }

        private void DisplayUser()
        {

            this.Text = "与" + _FriendList[0].NickName + "对话中";
            //picFriendHeader.Image = AppSetter.AllHeaders.Images[_FriendList[0].FaceId];
            lblUserTitle.Text = _CurrentFriend.NickName + "(" + _FriendList[0].Id + ")";

        }
        private void frmChat_Load(object sender, EventArgs e)
        {
            //this.timer1.Enabled = true;
            
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(txtMessage.Text.Trim()))
                {
                    if (this.getSendList != null)
                    {
                        _FriendList = getSendList();
                    }
                    if (_FriendList.Count > 0)
                    {
                        MessagesInfo obj = new MessagesInfo();
                        obj.FromUser = AppSetter.CurrentUser;
                        obj.ToUserList = _FriendList;
                        obj.Message = txtMessage.Text.Trim();
                        obj.MessageState = (int)EnumMessageState.NotRead;
                        Messages.SendMessage(obj, EnumMessageType.ChatMessage);
                        ShowMsgOnHistory();
                    }
                    else
                    {
                        MessageBox.Show("至少要选择一个用户", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }

                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            List<MessagesInfo> _list = Messages.GetMyNotReadMessages(AppSetter.CurrentUser.Id, EnumMessageType.ChatMessage);
            foreach (MessagesInfo obj in _list)
            {
                _ReceiveMessage = obj;
                _CurrentFriend = obj.FromUser;
                ShowMessage();
            }

        }

        private void ShowMsgOnHistory()
        {
            if (_FriendList.Count == 1)
            {
                this.txtMsgHistory.AppendText(string.Format(_ChatSendStyle, _FriendList[0].NickName, DateTime.Now));
                this.txtMsgHistory.AppendText(string.Format(this.txtMessage.Text + "\r\n"));
                txtMessage.Text = string.Empty;
            }
            else
            {
                this.txtMsgHistory.AppendText(string.Format(_ChatSendStyle, "所有人", DateTime.Now));
                this.txtMsgHistory.AppendText(string.Format(this.txtMessage.Text + "\r\n"));
                txtMessage.Text = string.Empty;
            }

        }

        private void btnToolRecords_Click(object sender, EventArgs e)
        {
            if (_FriendList!= null)
            {
                txtRecords.Text = Users.GetRecords(AppSetter.CurrentUser.Id, _FriendList[0].Id);
            }
            ShowRecords(showRecords);
            
        }
        private void ShowRecords(bool isShow)
        {
            //splitContainer1.Panel2Collapsed = true;
            if (isShow)
            {
                int a = this.Width;
                //splitContainer1.SplitterDistance = this.Width;
                this.Width = this.Width + 400;
                showRecords = false;
                splitContainer1.Panel2Collapsed = false;
                splitContainer1.SplitterDistance = a;
            }
            else
            {
               
                this.Width = this.Width - 400;
                int a = this.Width;
                ////splitContainer1.SplitterDistance = this.Width;
                showRecords = true;
                splitContainer1.SplitterDistance = a;
                splitContainer1.Panel2Collapsed = true;               


            }
        }

        private void txtMessage_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode  == Keys.Enter)
            {
                btnSend.PerformClick();
            }
        }

        private void frmChat_FormClosing(object sender, FormClosingEventArgs e)
        {
            SetTreeUser(sender, e);
        }

        private void btnRecordByTime_Click(object sender, EventArgs e)
        {
            if (_FriendList != null&&dateEditStart.Text !=string.Empty&&dateEditEnd.Text !=string.Empty)
            {
                DateTime end = new DateTime(dateEditEnd.DateTime.Year, dateEditEnd.DateTime.Month, dateEditEnd.DateTime.Day, 23, 59, 59);
                txtRecords.Text = Users.GetRecords(AppSetter.CurrentUser.Id, _FriendList[0].Id, dateEditStart.DateTime, end);
            }
        }
    }
}